<template>
	<div>
		<div class="order-line margin border select-container">
			<span class="fl">重新分配给</span>
			<select class="fr"
			:class="{empty: !selected}"
			v-model="selected">
				<option v-for="d in dept"
				:value="d.id"
				v-show="d.id">{{d.name}}</option>
			</select>
		</div>
		<div class="order-btn-container margin">
			<div class="c-btn big black"
			@click="popshow = true">取消该报修</div>
			<div class="c-btn big reverse"
			:class="{disabled: !selected || uploading}"
			@click="dispatch">确认二次分配</div>
		</div>
		<pop :popshow="popshow"
		:popstyle="{width:'8.5rem'}">
			<txtpop title="取消原因"
			:maxlength="200"
			placeholder="请输入取消原因"
			@cancel="popshow = false"
			@send="cancel"></txtpop>
		</pop>
	</div>
</template>
<script>
import {myHttp, myAlert, nextPage, getParameterByName} from '../assets/js/utils.js'
import {getDept} from '../assets/js/global.js'
import {tip} from '../assets/js/tip.js'
import pop from '../Pop.vue'
import txtpop from '../TxtPop.vue'
export default{
	name: 'Dispatcher',
	mounted(){
		getDept().then((depts) => {
			if(depts){
				Object.values(depts).forEach((e) => {
					this.dept.push(e)
				})
				// this.dept.concat(Object.values(depts));
			}
		})
	},
	data(){
		return{
			dept:[{
				id:'',
				name:'请选择要分配的部门'
			}],
			selected: '',
			popshow: false,
			uploading: false
		}
	},
	methods:{
		dispatch(){
			if(this.uploading) return;
			myHttp('setRepairDept.do',{
				repairId: this.$route.params.id,
				operateUserId: localStorage.getItem('userId'),
				orgId: this.selected
			},() => {
				this.uploading = true;
			}, {tip: true}).then((r) => {
				if(r.failed) {
					this.uploading = false;
					return;
				}
				myAlert.small(tip.DISPATCHER_SEND).then(() => {
					if(history.length == 1) this.$router.push({path:'/repair/list/dispatcher'});
					else nextPage(this.$router, this.$route);
				})
			})
		},
		cancel(txt){
			if(this.uploading) return;
			if(!txt) {
				myAlert.small('请输入取消原因');
				return;
			}
			myHttp('dispatchClosedRepair.do',{
				repairId: this.$route.params.id,
				userId: localStorage.getItem('userId'),
				closedReason: txt
			}, () => {
				this.uploading = true;
			}, {tip: true}).then((r) => {
				if(r.failed) {
					this.uploading = false;
					return;
				}
				myAlert.small(tip.DISPATCHER_CANCEL).then(() => {
					nextPage(this.$router, this.$route);
				})
			})
		}
	},
	components: {pop, txtpop}
}
</script>